//
//  YKEditPerformanceExchangeViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/10/15.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKEditPerformanceExchangeViewController.h"
#import "YKInputView.h"
#import "YKPopupView.h"
#import "YKInputMultilineView.h"
@interface YKEditPerformanceExchangeViewController ()
@property (nonatomic, strong)YKInputView *scoreView;
@property (nonatomic, strong)YKInputMultilineView *exchangeContentView;
@end

@implementation YKEditPerformanceExchangeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"申请兑换";
    // Do any additional setup after loading the view.
    YKInputView *requestNoView = [[YKInputView alloc]initWithTitle:@"申请单号" input:self.mYKPerformanceExchangeDetailModel.APPLY_CODE];
    requestNoView.editable = NO;
    [self.view addSubview:requestNoView];
    
    YKInputView *requestPersonView = [[YKInputView alloc]initWithTitle:@"申请人" input:self.mYKPerformanceExchangeDetailModel.APPLY_PERSON];
    requestPersonView.editable = NO;
    [self.view addSubview:requestPersonView];
    
    YKInputView *requestDateView = [[YKInputView alloc]initWithTitle:@"申请时间" input:self.mYKPerformanceExchangeDetailModel.APPLY_DATE];
    requestDateView.editable = NO;
    [self.view addSubview:requestDateView];
    
    YKInputView *scoreView = [[YKInputView alloc]initWithTitle:@"积分兑换" input:self.mYKPerformanceExchangeDetailModel.EXCHANGE_POINT];
    [self.view addSubview:scoreView];
    
    YKInputView *scoreLeftView = [[YKInputView alloc]initWithTitle:@"积分余额" input:self.mYKPerformanceExchangeDetailModel.ACHIEVEMENT_INTEGRAL];
    scoreLeftView.editable = NO;
    [self.view addSubview:scoreLeftView];
    
    YKInputMultilineView *exchangeContentView = [[YKInputMultilineView alloc]initWithTitle:@"兑换内容" input:self.mYKPerformanceExchangeDetailModel.EXCHANGE_CONTENT];
    [self.view addSubview:exchangeContentView];
    
    [self.view.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view.mas_leading).offset(13);
        make.trailing.equalTo(self.view.mas_trailing).offset(-13);
    }];
    [self.view.subviews mas_alignAlongAxis:MASAxisTypeVertical fixedSpace:@8 leadSpace:11];
    self.scoreView = scoreView;
    self.exchangeContentView = exchangeContentView;
    
    UIButton* saveButton = [[UIButton alloc]init];
    [self.view addSubview:saveButton];
    UIButton *cancleButton = [[UIButton alloc]init];
    [self.view addSubview:cancleButton];
    UIButton *submitButton = [[UIButton alloc]init];
    [self.view addSubview:submitButton];
    [saveButton setTitle:@"保存" forState:UIControlStateNormal];
    [cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    [submitButton setTitle:@"提交审批" forState:UIControlStateNormal];
    [saveButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [cancleButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [submitButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [saveButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [cancleButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [submitButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    saveButton.titleLabel.font = [UIFont systemFontOfSize:14];
    cancleButton.titleLabel.font = [UIFont systemFontOfSize:14];
    submitButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [saveButton addTarget:self action:@selector(onSaveButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [cancleButton addTarget:self action:@selector(onCancleButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [submitButton addTarget:self action:@selector(onSubmitButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    [@[saveButton,cancleButton, submitButton] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:1 leadSpacing:0 tailSpacing:0];
    [@[saveButton,cancleButton, submitButton] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view);
        make.height.mas_equalTo(44);
    }];
}
- (void)onSaveButtonTapped:(id)sender {
    int left = self.mYKPerformanceExchangeDetailModel.ACHIEVEMENT_INTEGRAL.integerValue;
    int original = self.mYKPerformanceExchangeDetailModel.EXCHANGE_POINT.integerValue;
    int newScore = self.scoreView.input.integerValue;
    if (left + original - newScore > 0) {
        NSString *id_p = self.mYKPerformanceExchangeDetailModel.ID;
        NSString *exchange_point = self.scoreView.input;
        NSString *original_exchange_point = self.mYKPerformanceExchangeDetailModel.EXCHANGE_POINT;
        NSString *achievement_integral = self.mYKPerformanceExchangeDetailModel.ACHIEVEMENT_INTEGRAL;
        NSString *exchange_content = self.exchangeContentView.input;
        NSString *apply_person = self.mYKPerformanceExchangeDetailModel.APPLY_PERSON;
        
        [TFHTTPMANAER POST:@"achievementPointsExchange/edtAchievementPointsExchange.htm" parameters:@{@"id":id_p, @"exchange_point":exchange_point, @"original_exchange_point":original_exchange_point, @"achievement_integral":achievement_integral, @"exchange_content":exchange_content, @"apply_person":apply_person} HUD:@"编辑中..." success:^(id responseObject) {
            [self.navigationController popViewControllerAnimated:YES];
            [self showHint:@"编辑成功"];
        } failure:^(id responseObject, NSError *error) {
            [self showHint:@"编辑失败,请点击重试"];
        }];
    }
    else {
        [self showHint:@"积分不够"];
    }
}

- (void)onCancleButtonTapped:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)onSubmitButtonTapped:(id)sender {
    
}

@end
